package com.ssy.lingxi.sms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.sms.api.common.ServiceTypeEnum;
import com.ssy.lingxi.sms.api.common.SmsTemplateEnum;
import com.ssy.lingxi.sms.api.common.TencentConfigEnum;
import com.ssy.lingxi.sms.api.model.SmsConfigVO;
import com.ssy.lingxi.sms.api.model.request.DeleteConfigRequest;
import com.ssy.lingxi.sms.api.model.request.EnableConfigRequest;
import com.ssy.lingxi.sms.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.sms.api.model.request.TemplateRequest;
import com.ssy.lingxi.sms.api.model.response.MapResponse;
import com.ssy.lingxi.sms.api.model.response.TemplateResponse;
import com.ssy.lingxi.sms.entity.SmsConfig;
import com.ssy.lingxi.sms.entity.Template;
import com.ssy.lingxi.sms.service.ISmsConfigService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 短信服务器配置类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/10
 */
@RestController
@RequestMapping("/sms")
public class ConfigSmsController {

    @Resource
    private ISmsConfigService smsConfigService;

    /**
     * 查询当前配置的服务商类型
     * @return 服务商类型: 1-腾讯云，2-阿里云
     */
    @RequestMapping(value = "/getServiceType", method = RequestMethod.GET)
    public Wrapper<Integer> getServiceType() {
        //查询生效的服务器配置
        SmsConfig smsConfig = smsConfigService.findEnableSmsConfig();
        if(smsConfig != null){
            return Wrapper.success(smsConfig.getServiceType());
        }else{
            return Wrapper.success(ServiceTypeEnum.Tencent.getCode());
        }
    }

    /**
     * 查询参数代码
     * @param serviceType 服务商类型: 1-腾讯云，2-阿里云
     * @return SmsConfig
     */
    @RequestMapping(value = "/getParamCode", method = RequestMethod.GET)
    public Wrapper<List<MapResponse>> getParamCode(@RequestParam("serviceType") Integer serviceType) {
        if(serviceType.equals(ServiceTypeEnum.Tencent.getCode())){
            return Wrapper.success(TencentConfigEnum.getCodes());
        }else if(serviceType.equals(ServiceTypeEnum.Ali.getCode())){
            return Wrapper.success(TencentConfigEnum.getCodes());
        }
        return Wrapper.success();
    }

    /**
     * 查询配置信息
     * @param serviceType 服务商类型: 1-腾讯云，2-阿里云
     * @return SmsConfig
     */
    @RequestMapping(value = "/getSmsConfig", method = RequestMethod.GET)
    public Wrapper<SmsConfig> getSmsConfig(@RequestParam("serviceType") Integer serviceType) {
        return Wrapper.success(smsConfigService.getSmsConfig(serviceType));
    }

    /**
     * 添加/修改短信服务器配置
     * @param smsConfigVO 配置参数
     * @return 是否成功
     */
    @RequestMapping(value = "/saveOrUpdateSmsConfig", method = RequestMethod.POST)
    public Wrapper<Boolean> saveOrUpdateSmsConfig(@RequestBody SmsConfigVO smsConfigVO) {
        return Wrapper.success(smsConfigService.saveOrUpdateSmsConfig(smsConfigVO));
    }

    /**
     * 删除短信服务器配置
     * @param deleteConfigRequest 参数
     * @return 是否成功
     */
    @RequestMapping(value = "/deleteSmsConfig", method = RequestMethod.POST)
    public Wrapper<Boolean> deleteSmsConfig(@RequestBody DeleteConfigRequest deleteConfigRequest) {
        Integer serviceType = deleteConfigRequest.getServiceType();
        String code = deleteConfigRequest.getCode();
        return Wrapper.success(smsConfigService.deleteSmsConfig(serviceType, code));
    }

    /**
     * 启用/停用短信服务器
     * @param enableConfigRequest 参数
     * @return 是否成功
     */
    @RequestMapping(value = "/updateSmsConfigStatus", method = RequestMethod.POST)
    public Wrapper<Boolean> updateSmsConfigStatus(@RequestBody EnableConfigRequest enableConfigRequest) {
        Integer serviceType = enableConfigRequest.getServiceType();
        return Wrapper.success(smsConfigService.updateSmsConfigStatus(serviceType));
    }

    /**
     * 查询功能模块代码
     * @return SmsConfig
     */
    @RequestMapping(value = "/getModuleCode", method = RequestMethod.GET)
    public Wrapper<List<MapResponse>> getModuleCode() {
        return Wrapper.success(SmsTemplateEnum.getCodes());
    }

    /**
     * 查询短信内容模板
     * @param pageVO 分页实体
     * @return 短信内容模板
     */
    @RequestMapping(value = "/getSmsTemplateList", method = RequestMethod.GET)
    public Wrapper<PageData<TemplateResponse>> getSmsTemplateList(PageVO pageVO) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize());
        Page<Template> result = smsConfigService.getSmsTemplateList(page);
        List<TemplateResponse> templateResponseList = result.getContent().stream().map(tencentTemplate -> BeanUtil.copyProperties(tencentTemplate, TemplateResponse.class)).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), templateResponseList));
    }

    /**
     * 新增/修改短信内容模板
     * @param templateRequest 模板参数
     * @return 是否成功
     */
    @RequestMapping(value = "/saveOrUpdateSmsTemplate", method = RequestMethod.POST)
    public Wrapper<Boolean> saveOrUpdateSmsTemplate(@RequestBody TemplateRequest templateRequest) {
        return Wrapper.success(smsConfigService.saveOrUpdateSmsTemplate(templateRequest));
    }

    /**
     * 删除短信内容模板
     * @param simpleIdRequest 短信内容模板id
     * @return 是否成功
     */
    @RequestMapping(value = "/deleteSmsTemplate", method = RequestMethod.POST)
    public Wrapper<Boolean> deleteSmsTemplate(@RequestBody SimpleIdRequest simpleIdRequest) {
        return Wrapper.success(smsConfigService.deleteSmsTemplate(simpleIdRequest.getId()));
    }

    /**
     * 清空短信内容模板
     * @return 是否成功
     */
    @RequestMapping(value = "/clearSmsTemplate", method = RequestMethod.POST)
    public Wrapper<Boolean> clearSmsTemplate() {
        return Wrapper.success(smsConfigService.clearSmsTemplate());
    }

}
